﻿bob.ns.setNs('bob.event', {

    //v.2
    Event: function () {
        var self = this;

        var _handlers = [];

        this.add_handler = function (fnHandler) {
            if (!bob.utils.isFunction(fnHandler)) {
                throw new Error('Invalid argument: fnHandler.');
            }

            _handlers.push(fnHandler);
            return fnHandler;
        };
        this.remove_handler = function (fnHandler) {
            bob.array.extensions.remove(_handlers, fnHandler, true);
        };

        var _fire = function () {
            var eventArguments = arguments;
            var handlersClone = bob.collections.extensions.clone.call(_handlers);
            bob.collections.extensions.forEach.call(handlersClone, function (index, fn) {
                fn.apply(self, eventArguments);
            });
        };

        this.get_caller = function (bRetain) {
            if (!bRetain) {
                delete self.get_caller;
            }

            return _fire;
        };
    }

});
